/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parsers;

/**
 *
 * @author Alejandro
 */
public class ExtensionPanelParser {

    public enum State {
        LocalizedString, ControlName, ReplaceName, Text, Finished
    };
    private String source;
    private String argument;
    private int i = -1;
    private int startGroup = 0;
    private int endGroup = 0;
    private State lastState = null;

    public ExtensionPanelParser(String source) {
        this.source = source;
    }

    public char getChar() {
        if (lastState.equals(State.Text)) {
            return source.charAt(i);
        }
        return 0;
    }

    public String getArgument() {
        if (lastState == State.LocalizedString || lastState == State.ControlName || lastState == State.ReplaceName)
            return argument;
        return null;
    }

    private State parseState() {
        String s = source.substring(startGroup, endGroup+1);
        int space = 0;
        for (int j=0; j<s.length(); j++)
            if (s.charAt(j) == ' ') {
                space = j;
                break;
            }
        if (space != 0) {
            String prefix = s.substring(1, space);
            argument = s.substring(space+1, s.length()-1);
            if (prefix.equals("#STRING")) {
                return State.LocalizedString;
            } else if (prefix.equals("#CONTROL")) {
                return State.ControlName;
            } else if (prefix.equals("#REPLACE")) {
                return State.ReplaceName;
            }
        }
        return null;
    }

    public State run() {
        if (source.length() > i) {
            ++i;
            if (source.length() == i) {
                lastState = State.Finished;
            } else {
                boolean bBkSlash = false;
                boolean bEnd = false;
                boolean bLock = false;
                while (!bEnd) {
                    char c = source.charAt(i);
                    switch (c) {
                        case '[': {
                            if (!bBkSlash) {
                                bLock = true;
                                startGroup = i;
                                ++i;
                            } else {
                                bBkSlash = false;
                                lastState = State.Text;
                                bEnd = true;
                            }
                        }
                        break;
                        case ']': {
                            if (!bBkSlash) {
                                bLock = false;
                                endGroup = i;
                                lastState = parseState();
                                bEnd = true;
                            } else {
                                bBkSlash = false;
                                lastState = State.Text;
                                bEnd = true;
                            }
                        }
                        break;
                        case '\\': {
                            if (!bBkSlash) {
                                bBkSlash = true;
                                ++i;
                            } else if (!bLock) {
                                bBkSlash = false;
                                lastState = State.Text;
                                bEnd = true;
                            } else if (bLock) {
                                ++i;
                            }
                        }
                        break;
                        default: {
                            if (!bLock) {
                                lastState = State.Text;
                                bEnd = true;
                            } else {
                                ++i;
                            }
                        }
                        break;
                    }
                }
            }
        }
        return lastState;
    }
}
